'''
This is an example of how to use the package modules.

Notice that Collective2 is not a free simulation package. 
Created on May 14, 2012
@author: bruce
'''
from datetime import date

from src.dailystock.datacollect.dailyupdate import DailyUpdate
from src.dailystock.datacollect.histquotes import HistQuotes
from src.dailystock.indicators.plot_histdata import HistDataPlot
from src.dailystock.strategies.ma_simple import MASimple
from src.trade.collective2 import Collective2 


def dailyup_run(output_dir):
    daily_up = DailyUpdate(output_dir)
    daily_up.run()

def histquotes_run(output_dir):
    sp500_symbols_file_name = "SPY500.list"      
    sp500_hist = HistQuotes(sp500_symbols_file_name, output_dir)
    sp500_hist.run()
    
def histdataplot_run(output_dir, symbols):
    dataplot = HistDataPlot(output_dir)
    for sym in symbols:
        dataplot.plot(sym, [('c', 0), ('ma', 5), ('ema', 10)], date(2012, 1, 1))   

def masimple_run(csv_dir="input\\"):
    masimple = MASimple(True, csv_dir)
    return masimple.run()     

def collective2_run(closelongs, addlongs):
    mySystemId = "00000007"
    email = "jamesbond@bond.com"
    pw = "iambond"    
    c2 = Collective2(email,pw)
    for symbol_close in closelongs:
        args = dict(systemid = mySystemId, action = c2.SELL_TO_CLOSE, quant = 10, 
                        instrument = c2.STOCK, symbol = symbol_close, duration = c2.DAY)
        c2.place_order(**args)
    for symbol_add in addlongs:
        args = dict(systemid = mySystemId, action = c2.BUY_TO_OPEN, quant = 10, 
                        instrument = c2.STOCK, symbol = symbol_add, duration = c2.DAY)        
        c2.place_order(**args)
    
if __name__ == '__main__':    
    output_dir = "james bond's business directory\\csv_dir\\"    
    histquotes_run(output_dir) # this only needs to be run once entirely
    
    # daily update, calculate from the strategy and start real time simulation.
    dailyup_run(output_dir)    
    (currlongs, closelongs, addlongs) = masimple_run(output_dir)
    # optional: plot the changed stocks to confirm the signal 
    histdataplot_run(output_dir, addlongs)        
    collective2_run(closelongs, addlongs)    
        